describe('package-lock.json', () => {
  /** @type {import('./package-lock.json')} */
  let packageLockJson

  beforeAll(async () => {
    packageLockJson = await import('./package-lock.json')
  })

  describe('Dependabot updates', () => {
    /**
     * When this check fails, the incorrect `package-lock.json` can be repaired
     * by checking out the Dependabot branch and running `npm install` to remove
     * the hoisted dependency
     *
     * {@link https://govuk-design-system-team-docs.netlify.app/how-we-work/version-control/pull-requests#reviewing-a-pr-from-dependabot}
     */
    it("should not hoist 'optionalDependencies' to 'dependencies'", () => {
      const { dependencies, optionalDependencies } =
        packageLockJson.packages['']

      // List package names for comparison
      const packageNames = Object.keys(dependencies ?? {})
      const packageNamesOptional = Object.keys(optionalDependencies ?? {})

      // Check no optional dependencies are hoisted
      for (const name of packageNamesOptional) {
        expect(packageNames).not.toContain(name)
      }
    })
  })
})
